package com.xiang.utils;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.xiang.constant.DealBaseInfo;
import com.xiang.constant.DealBusinesses;
import com.xiang.constant.DistrictsBaseinfo;
import com.xiang.constant.RegionsBaseinfo;

public class JsonUtil {
	/**
	 * 解析城市列表
	 */
	public static  List<String> analysisCities(String jsonStr) {
		List<String> cityList = new ArrayList<String>();
		try {
			JSONObject object= new JSONObject(jsonStr);
			JSONArray array = object.getJSONArray("cities");
			for(int i=0; i<array.length(); i++){
//				System.out.println("#######city: "+array.getString(i));
				cityList.add(array.getString(i));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return cityList;
	}
	
	/**
	 * 解析最新城市下属区域列表
	 */
	public static  RegionsBaseinfo analysisRegions(String jsonStr) {
		RegionsBaseinfo info = new RegionsBaseinfo();
		try {
			JSONObject object= new JSONObject(jsonStr);
			String citiesStr = object.getString("cities");
			JSONArray array = new JSONArray(citiesStr);
			JSONObject object1 =  (JSONObject) array.get(0);
			info.city_name = object1.getString("city_name");
//			System.out.println("#######city_name: "+info.city_name);
			
			List<DistrictsBaseinfo> districtsList = new ArrayList<DistrictsBaseinfo>();
			JSONArray districtsArray = object1.getJSONArray("districts");
			for(int i = 0; i< districtsArray.length(); i++){
				DistrictsBaseinfo districtsBaeinfo = new DistrictsBaseinfo();
				List<String> neighborhoodsList = new ArrayList<String>();
				districtsBaeinfo.district_name = districtsArray.getJSONObject(i).getString("district_name");
//				System.out.println("#######district_name: "+districtsBaeinfo.district_name);
				JSONArray neighborhoodsArray = districtsArray.getJSONObject(i).getJSONArray("neighborhoods");
				for(int k=0; k<neighborhoodsArray.length(); k++){
					String name = neighborhoodsArray.getString(k);
					neighborhoodsList.add(name);
//					System.out.println("#######neighborhoodsList_name: "+name);
				}
				districtsBaeinfo.neighborhoodsList = neighborhoodsList;
				districtsList.add(districtsBaeinfo);
			}
			info.districtsList = districtsList;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return info;
	}
	/**
	 * 解析团购信息列表
	 */
	public static  List<DealBaseInfo> analysisDeal(String jsonStr) {
		 List<DealBaseInfo> list = new ArrayList<DealBaseInfo>();
		try {
			JSONObject object= new JSONObject(jsonStr);
			String deal = object.getString("deals");
			JSONArray array = new JSONArray(deal);
			int size = array.length();
			for(int i=0; i<size; i++){
				DealBaseInfo baseinfo = new DealBaseInfo();
				baseinfo.deal_id = array.getJSONObject(i).getString("deal_id");
				baseinfo.title = array.getJSONObject(i).getString("title");
//				System.out.println("########tittle: "+baseinfo.title);
				baseinfo.description = array.getJSONObject(i).getString("description");
				baseinfo.list_price = array.getJSONObject(i).getString("list_price");
				baseinfo.current_price = array.getJSONObject(i).getString("current_price");
				baseinfo.regions = array.getJSONObject(i).getString("regions");
				baseinfo.purchase_count = array.getJSONObject(i).getString("purchase_count");
				baseinfo.purchase_deadline = array.getJSONObject(i).getString("purchase_deadline");
				baseinfo.publish_date = array.getJSONObject(i).getString("publish_date");
				baseinfo.distance = array.getJSONObject(i).getString("distance");
				baseinfo.image_url = array.getJSONObject(i).getString("image_url");
				baseinfo.s_image_url = array.getJSONObject(i).getString("s_image_url");
				baseinfo.deal_url = array.getJSONObject(i).getString("deal_url");
				baseinfo.deal_h5_url = array.getJSONObject(i).getString("deal_h5_url");
				
				JSONArray businessesArray = array.getJSONObject(i).getJSONArray("businesses");
				List<DealBusinesses> dealBusinessesList = new ArrayList<DealBusinesses>();
				for(int k = 0; k<businessesArray.length(); k++){
					DealBusinesses dealBusinesses = new DealBusinesses();
					dealBusinesses.name = businessesArray.getJSONObject(k).getString("name");
//					System.out.println("########name: "+dealBusinesses.name);
					dealBusinesses.id = businessesArray.getJSONObject(k).getString("id");
					dealBusinesses.url = businessesArray.getJSONObject(k).getString("url");
					dealBusinesses.h5_url = businessesArray.getJSONObject(k).getString("h5_url");
					dealBusinessesList.add(dealBusinesses);
				}
				baseinfo.businesses = dealBusinessesList;
				list.add(baseinfo);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 解析团购条数
	 */
	public static  int analysisDeal_count(String jsonStr) {
		int  total_count = 0;
		try {
			JSONObject object= new JSONObject(jsonStr);
			total_count = object.getInt("total_count");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return total_count;
	}
}
